package com.koo.modules.base.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.koo.modules.product.entity.vo.ProductQueryVo;
import com.koo.modules.sale.entity.vo.SaleQueryVo;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 说明：
 *
 * @author: charlin
 * @date: 2022-07-19 15:47
 * @version: 1.0.1
 */
public class WrapperUtil {
    public static void initQueryWrapper(Map<String, Object> params, QueryWrapper wrapper) {
        if (params.get("dataForm") != null) {
            //1、转换为字符串
            String dataForm = params.get("dataForm") + "";
            //2、转换为对象
            ProductQueryVo queryVo = JSON.parseObject(dataForm, ProductQueryVo.class);

            if (queryVo != null) {
                Long categoryId = queryVo.getCategoryId();
                String key = queryVo.getKey();
                String queryField = queryVo.getQueryField();
                String unit = queryVo.getUnit();
                String supplierName = queryVo.getSupplierName();
                String priceField = queryVo.getPriceField();
                String priceFormula = queryVo.getPriceFormula();
                BigDecimal priceValue = queryVo.getPriceValue();
                String selectField = queryVo.getSelectField();
                Integer selectValue = queryVo.getSelectValue();
                //3、根据类型查询
                wrapper.eq(!StrUtil.isEmptyIfStr(categoryId), "category_id", categoryId);
                //4、根据关键词查询
                wrapper.like(StringUtils.isNotBlank(queryField) && StringUtils.isNotBlank(key), queryField, key);
                //5、根据单位查询
                wrapper.eq(StringUtils.isNotBlank(unit), "unit", unit);
                //6、根据供应商名称查询
                wrapper.eq(StringUtils.isNotBlank(supplierName), "supplier_name", supplierName);
                //7、根据价格、库存、毛利查询
                if (StringUtils.isNotBlank(priceFormula)) {
                    if ("GE".equalsIgnoreCase(priceFormula)) {
                        wrapper.ge(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("EQ".equalsIgnoreCase(priceFormula)) {
                        wrapper.eq(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("GT".equalsIgnoreCase(priceFormula)) {
                        wrapper.gt(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("LE".equalsIgnoreCase(priceFormula)) {
                        wrapper.le(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("LT".equalsIgnoreCase(priceFormula)) {
                        wrapper.lt(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("NE".equalsIgnoreCase(priceFormula)) {
                        wrapper.ne(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                }
                //8、帅选查询
                wrapper.eq(StringUtils.isNotBlank(selectField) && selectValue != null, "IFNULL(" + selectField + ",0)", selectValue);
            }
        }
    }

    public static void initSaleQueryWrapper(Map<String, Object> params, QueryWrapper wrapper) {
        if (params.get("dataForm") != null) {
            //1、转换为字符串
            String dataForm = params.get("dataForm") + "";
            //2、转换为对象
            SaleQueryVo queryVo = JSON.parseObject(dataForm, SaleQueryVo.class);

            if (queryVo != null) {
                Integer payType = queryVo.getPayType();
                String saleUser = queryVo.getSaleUser();
                String key = queryVo.getKey();
                String queryField = queryVo.getQueryField();
                String priceField = queryVo.getPriceField();
                String priceFormula = queryVo.getPriceFormula();
                BigDecimal priceValue = queryVo.getPriceValue();
                String selectField = queryVo.getSelectField();
                Integer selectValue = queryVo.getSelectValue();
                //3、根据销售类型查询
                wrapper.eq(!StrUtil.isEmptyIfStr(payType), "pay_type", payType);
                //4、根据关键词查询
                wrapper.like(StringUtils.isNotBlank(queryField) && StringUtils.isNotBlank(key), queryField, key);
                //5、根据营业员查询
                wrapper.eq(StringUtils.isNotBlank(saleUser), "sale_user", saleUser);
                //7、根据价格、库存、毛利查询
                if (StringUtils.isNotBlank(priceFormula)) {
                    if ("GE".equalsIgnoreCase(priceFormula)) {
                        wrapper.ge(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("EQ".equalsIgnoreCase(priceFormula)) {
                        wrapper.eq(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("GT".equalsIgnoreCase(priceFormula)) {
                        wrapper.gt(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("LE".equalsIgnoreCase(priceFormula)) {
                        wrapper.le(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("LT".equalsIgnoreCase(priceFormula)) {
                        wrapper.lt(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                    if ("NE".equalsIgnoreCase(priceFormula)) {
                        wrapper.ne(StringUtils.isNotBlank(priceField) && priceValue != null, "IFNULL(" + priceField + ",0)", priceValue);
                    }
                }
                //8、帅选查询
                wrapper.eq(StringUtils.isNotBlank(selectField) && selectValue != null, "IFNULL(" + selectField + ",0)", selectValue);
            }
        }
    }
}
